package top.honlnk.controller;

import jakarta.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import top.honlnk.common.constant.PublicConstant;
import top.honlnk.pojo.dto.KeysValidateDTO;
import top.honlnk.pojo.vo.AdminGetRotaVO;
import top.honlnk.pojo.vo.KeyValidationResultVO;
import top.honlnk.common.result.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;
import top.honlnk.service.AdminService;
import top.honlnk.service.PublicService;

import java.util.List;

@Slf4j
@RestController
@RequestMapping
public class publicController {
    @Autowired
    private PublicService publicService;

    @Autowired
    private AdminService adminService;
    @PostMapping("/keys/validate")
    public Result<List<KeyValidationResultVO>> validateKeysList(@RequestBody KeysValidateDTO keysValidateDTO) {
        List<String> keys = keysValidateDTO.getKeys();
        if (keys.isEmpty()) {
            throw new RuntimeException(PublicConstant.KEYS_IS_NULL);
        }
        // 判断keys中的每个值的长度是否为11或者22位
        keys.forEach(key -> {
            if (key.length() != 11 && key.length() != 22) {
                throw new RuntimeException(key + PublicConstant.KEY_LENGTH_ERROR);
            }
        });
        return Result.success(publicService.validateKeysList(keys));
    }

    @GetMapping("/get/rota")
    public Result<AdminGetRotaVO> getRota(HttpServletRequest request){
        String key = request.getHeader("key");
        return Result.success(publicService.getRota(key));
    }
}
